package cn.edu.ctbu.studentmanage.api;

import cn.edu.ctbu.studentmanage.core.PageUtils;
import cn.edu.ctbu.studentmanage.core.RUtil;
import cn.edu.ctbu.studentmanage.domain.Student;
import cn.edu.ctbu.studentmanage.service.StudentService;
import cn.edu.ctbu.studentmanage.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class StudentApiController {

    @Autowired
    private StudentService studentService;


    /**
     * 读取的url:/api/studentindex
     *
     * @return
     */

    @GetMapping("/studentindex")
    public R findAll(){
        return RUtil.success(studentService.findAll());
    }

    /**
     * 读取的url:/api/get/sid
     *
     * @return
     */

    @GetMapping("/get/{sid}")
    public R get(@PathVariable Integer sid) {
        Student student = studentService.getBySid(sid);

        return RUtil.success(student);
    }

    /**
     * 新增学生的方法./api/insert
     * @param student
     */
    @PostMapping("/insert")
    public R insert(@Valid Student student, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            String errStr = bindingResult.getFieldError().getDefaultMessage();
            log.error(errStr);
        }
        studentService.Insert(student);

        log.info(student.toString());
        return RUtil.success(null);
    }


    /**
     * 更新学生的方法./api/update
     * @param student
     */
    @PutMapping ("/update")
    public R update(Student student){

        studentService.update(student);

        log.info(student.toString());
        return RUtil.success(null);
    }

    /**
     * 删除学生的方法./api/delete
     * @param sid
     */
    @DeleteMapping ("/delete/{sid}")
    public R delete(@PathVariable Integer sid){

        studentService.delete(sid);

//        log.info(student.toString());
        return RUtil.success(null);
    }

    /**
     *
     * 分页读取全部内容./api/getbypage
     * @return
     */
    @GetMapping("/getbypage")
    public R findByPage(@RequestParam(value = "page",defaultValue = "0")Integer page,
                        @RequestParam(value = "size",defaultValue = "10")Integer size,
                        @RequestParam(value = "sname",defaultValue = "")String sname,
                        @RequestParam(value = "ssex",defaultValue = "")String ssex,
                        @RequestParam(value = "sdept",defaultValue = "")String sdept
    ){

        Page<Student> studentPage ;
        if(StringUtils.isEmpty(sname) && StringUtils.isEmpty(sdept)&& StringUtils.isEmpty(ssex)
        ){
            Pageable pageable = PageRequest.of(page,size, Sort.by("sid"));
            studentPage =studentService.findAll(pageable);
        }else {
            Student student = new Student();
            student.setSname(sname);
            student.setSsex(ssex);
            student.setSdept(sdept);

            Pageable pageable = PageRequest.of(page,size,Sort.by("sid"));
            ExampleMatcher matcher =ExampleMatcher.matching()
                    .withMatcher("sname",ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("ssex",ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("sdept",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student> example = Example.of(student,matcher);
//        Example<Student> example = Example.of(student);
            studentPage = studentService.findAll(example,pageable);
        }

        PageUtils pageUtils = new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return RUtil.success(pageUtils);
    }
}